import java.util.*;

/**
 * @Author wonghiu
 * @Date 2021/8/15 20:07
 * @Version 1.0.1
 *
 * 3
 * 2 2
 * 1 1 2 2
 * ..
 * #.
 * 3 3
 * 1 1 3 3
 * ..#
 * .#.
 * #..
 * 4 4
 * 1 1 3 3
 * ....
 * ..#.
 * .#.#
 * ...#
 *
 */
public class mihayo {
    private static int resMin = Integer.MAX_VALUE;
    private static int t = 0;
    private static int[][] visit;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        for (int i = 0; i < n; i++) {
            t = 0;
            resMin = Integer.MAX_VALUE;
            int sizei = sc.nextInt();
            int sizej = sc.nextInt();
            int starti = sc.nextInt();
            int startj = sc.nextInt();
            int endi = sc.nextInt();
            int endj = sc.nextInt();
            Character[][] matrix = new Character[sizei][sizej];
            visit = new int[sizei][sizej];
            System.out.println("");
            for (int ii = 0; ii < sizei; ii++) {
                String next = sc.next();
                for (int jj = 0; jj < next.length(); jj++) {
                    matrix[ii][jj] = next.charAt(jj);
                }
            }
            dfs(matrix,starti-1,startj-1,endi-1,endj-1);
            System.out.println(resMin==Integer.MAX_VALUE?-1:resMin);
        }
    }

    public static void dfs(Character[][] matrix, int starti, int startj, int endi, int endj){
        if (starti <0 || startj<0 || starti>matrix.length-1 || startj>matrix[0].length-1 || matrix[starti][startj]=='#'){
            return;
        }
        if (Math.abs(starti-endi)<=1 && Math.abs(startj-endj)<=1){
            resMin = Math.min(resMin,((starti+1)*(endi+1))^t^((startj+1)*(endj+1)));
            return;
        }
        if (visit[starti][startj]==1){
            return;
        }
        t++;
        Character temp = matrix[starti][startj];
        matrix[starti][startj]='#';
        dfs(matrix,starti-1,startj,endi,endj);
        dfs(matrix,starti,startj-1,endi,endj);
        dfs(matrix,starti+1,startj,endi,endj);
        dfs(matrix,starti,startj+1,endi,endj);
        t--;
        matrix[starti][startj]=temp;
        visit[starti][startj]=1;
    }
}
class solution{

    public boolean check(String str){
        while (true){
            int preLen = str.length();
            str = str.replace("ab", "");
            if (preLen==str.length()){
                break;
            }
        }
        if (str.length()==0){
            return true;
        }else {
            return false;
        }
    }

    public int check2(int[] nums){
        Map<Integer,Integer> map0 = new HashMap<>();
        Map<Integer,Integer> map1 = new HashMap<>();
        int count0 = 0;
        int count1 = 0;
        for (int i = 0; i < nums.length; i++) {
            if (i%2==0){
                map0.put(nums[i],map0.getOrDefault(nums[i],0)+1);
                count0++;
            }
            else {
                map1.put(nums[i],map1.getOrDefault(nums[i],0)+1);
                count1++;
            }
        }
        ArrayList<Integer> list0 = new ArrayList<>(map0.keySet());
        ArrayList<Integer> list1 = new ArrayList<>(map1.keySet());
        Collections.sort(list0,new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return map0.get(o2)-map0.get(o1);
            }
        });
        Collections.sort(list1 ,new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return map1.get(o2)-map1.get(o1);
            }
        });

        int i0 =0;
        int i1 =0;

        int res = count0-map0.get(list0.get(i0))+count1-map1.get(list1.get(i1));
        return res;
    }


}
